<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
          <el-form-item label="广告图片">
            <el-upload
              class="avatar-uploader"
              :action="fileUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess">
              <img
                v-if="form.ad_pic"
                :src="form.ad_pic"
                class="avatar"/>
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
      <el-form-item label="广告详情">
        <el-input v-model="form.ad_desc"  type="textarea"/>
      </el-form-item>
      <el-form-item label="网站实例">
        <el-select v-model="domain">
          <el-option
            v-for="item in domains"
            :key="item.id"
            :label="item.name"
            :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-if = "id > 0" type="primary" @click="onSubmit">修改</el-button>
        <el-button v-else type="primary" @click="onSubmit">添加</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {updateAdvertisement,getInfo} from '@/api/advertisement';
import {getList} from '@/api/domain';
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      form: {
       ad_pic: '',
       ad_desc: '',
       domain:''
      },
      id:0,
      domains:[],
      domain:''
    }
  },
  created() {
    this.id=this.$route.query.id
    if(this.id >0) {
      this.getFormData()
    }
    this.getDomains()
  },
  computed: {
    ...mapGetters(["fileUpload", "token"])
  },
  methods: {
    getFormData(){
      let params = {
        id:this.id
      }
      getInfo(params).then(response => {
        this.form = response.data
        this.domain = this.form.domain
      })
    },
    getDomains(){
      getList().then(response => {
        this.domains = response.data
      })
    },
    onSubmit() {
      let params = {
        id:this.id,
        ad_pic:this.form.ad_pic,
        ad_desc:this.form.ad_desc,
        domain:this.domain
      }
      updateAdvertisement(params).then(response => {
        if(this.id > 0){
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          });
        }else{
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          });
        }
      })
      this.$router.push({ path: `adTable`})
    },
    handleAvatarSuccess(response, file, fileList) {
      if (response && response.data && response.data.url) {
        this.$set(this.form, "ad_pic", response.data.url);
      }
    }
  }
}
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

